package com.demo.web;

import com.demo.common.Constants;
import com.demo.dto.BaseResult;
import com.demo.dto.ListResult;
import com.demo.dto.Pagination;
import com.demo.entity.*;
import com.demo.service.ILadingBillService;
import com.demo.service.IOfferService;
import com.demo.service.IStoreAccountService;
import com.demo.service.IStoreService;
import com.demo.vo.StoreVo;
import com.demo.entity.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin/store")
public class StoreController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IStoreService depService;
    @Autowired
    private IStoreAccountService saService;
    @Autowired
    private ILadingBillService ldService;
    @Autowired
    private IOfferService offerService;

    /**
     * 创建
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public BaseResult<Integer> create(Store arg) {
        int service_res = depService.insertSelective(arg);
        BaseResult<Integer> res = new BaseResult<Integer>(true, service_res);
        return res;
    }

    /**
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public BaseResult<Object> delete(Store arg) {
        arg.setStatus("0"); //逻辑删除
        LadingBill lb = new LadingBill();
        lb.setStoreId(arg.getId().toString());
        int num = ldService.selectByStoreId(lb);
        if (num > 0) {
            return new BaseResult(false, "仓库关联提单数据，无法删除", 403);
        }
        Offer offer = new Offer();
        offer.setDeliverPlaceIds(arg.getId().toString());
        int num_ = offerService.selectByDeliverPlaceIds(offer);
        if (num_ > 0) {
            return new BaseResult(false, "仓库关联报价数据，无法删除", 403);
        }
        int service_res = depService.updateByPrimaryKeySelective(arg);
        return new BaseResult<Object>(true, service_res);
    }

    /**
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult<Integer> update(Store arg) {
        int service_res = depService.updateByPrimaryKeySelective(arg);
        BaseResult<Integer> res = new BaseResult<Integer>(true, service_res);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/plus", method = RequestMethod.POST)
    public BaseResult<Object> plus(Store arg, Double number, HttpSession session) {
        StoreAccount sa = new StoreAccount();
        sa.setStoreId(String.valueOf(arg.getId()));
        User user = (User) session.getAttribute(Constants.USER);
        if (user == null) {
            return new BaseResult<Object>(false, "未登录!", 401);
        }
        sa.setNumberCanSell(number);

        Object sa_res = saService.plusNumber(sa, String.valueOf(user.getId()));
        BaseResult<Object> res = new BaseResult<Object>(true, sa_res);
        return res;
    }

    /***
     * 分页查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult query(StoreVo arg) {
        List<Store> list = depService.queryByPage(arg);
        int total = depService.queryCount(arg);
        arg.getPage();
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        System.out.println("Size:" + arg.getSize());
        System.out.println("getSortField:" + arg.getSortField());
        ListResult res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }
}
